package com.nuanshui.heatedloan.web.upload;

import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.res.UploadRequest;
import com.nuanshui.framework.res.ResourceFacade;
import com.nuanshui.framework.res.qiniu.UpLoadFileResult;
import com.nuanshui.framework.res.qiniu.UpLoadFileVO;
import com.nuanshui.framework.res.qiniu.UploadOptionFlag;
import com.nuanshui.heatedloan.model.UEditorResposeModel;
import com.nuanshui.heatedloan.web.ApiController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@RestController
@RequestMapping("/cms/manage/upload")
public class UploadController extends ApiController{
	

	@Autowired
	private ResourceFacade resourceFacade;

	//上传文件
	@RequestMapping("file")
	public String upload(
            @RequestParam("file") CommonsMultipartFile file,
			UploadRequest request) {
		try {
			request.setMultipartFile(file);
			UpLoadFileResult result=resourceFacade.uploadFile(request);

			if(request.getResponseFlag()== UploadOptionFlag.OPTION_RESPONSE_ALL)
			{
				return success(result);
			}
            if(request.getResponseFlag()== UploadOptionFlag.OPTION_RESPONSE_FILE)
			{
				return success(result.getFile());
			}
			else if(request.getResponseFlag()== UploadOptionFlag.OPTION_RESPONSE_IMAGE)
			{
				return success(result.getImage());
			}

		} catch (Exception e) {
			
			e.printStackTrace();
		} 
		return success();
	}

	@RequestMapping("editor")
	public String uEditorHandle(@RequestParam("upfile") CommonsMultipartFile upfile){
		try {
			UploadRequest request=new UploadRequest();
			request.setMultipartFile(upfile);
			UpLoadFileVO upLoadFileVO = resourceFacade.uploadFile(request).getFile();
			UEditorResposeModel model = new UEditorResposeModel();
			model.setState("SUCCESS");
			model.setUrl(upLoadFileVO.getUrl());
			return  JSON.toJSONString(model);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return success();
	}
}
